package com.guigu.chenjiajun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.chenjiajun.service.ProductionDispatchingService;
import com.guigu.chenjiajun.vo.MDesignProcedureModuleVo;
import com.guigu.chenjiajun.vo.MManufactureVo;

import com.guigu.chenjiajun.vo.MProcedureVo;
import com.guigu.common.mapper.*;
import com.guigu.common.pojo.*;
import com.guigu.common.util.GetUUidUtil;
import com.guigu.common.vo.SearchVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductionDispatchingServiceImpl implements ProductionDispatchingService {

    @Resource
    private GetUUidUtil getUUidUtil;
    @Resource
    private MManufactureMapper mManufactureMapper;
    @Resource
    private MApplyMapper mapper;
    @Resource
    private DFileMapper dFileMapper;

    @Resource
    private MDesignProcedureDetailsMapper mDesignProcedureDetailsMapper;

    @Resource
    private MDesignProcedureModuleMapper mDesignProcedureModuleMapper;

    @Override
    public SearchVo<MApply> queryDispatching(SearchVo searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(), searchVo.getPageSize());
        PageInfo p = new PageInfo(this.mapper.queryDispatching(searchVo));
        searchVo.setRows(p.getList());
        searchVo.setTotal(p.getTotal());
        return searchVo;
    }

    @Override
    public DFile showProduction(String id) {
        return this.dFileMapper.showProduction(id);
    }

    @Override
    public List<MDesignProcedureDetails> PreviewTheProcess(String id) {
        return this.mDesignProcedureDetailsMapper.PreviewTheProcess(id);
    }

    @Override
    public List<MDesignProcedureModule> CheckTheMaterial(String id) {

        return this.mDesignProcedureModuleMapper.CheckTheMaterial(id);
    }

    @Override
    public int SubmissionPreparationSheet(MManufactureVo mProcedureVo) {
        try {
            String s = this.getUUidUtil.returnId("301");
            mProcedureVo.setManufactureId(s);
            int sum=0;
            this.mManufactureMapper.insertReturnkey(mProcedureVo);
            for (int z = 0; z < mProcedureVo.getProcedureList().size(); z++) {
                MProcedureVo mProcedureVo1 = mProcedureVo.getProcedureList().get(z);
                mProcedureVo1.setParentId(mProcedureVo.getId());
                sum+=this.mManufactureMapper.selectByModule_cost_price_sum(mProcedureVo1.getDetailsNumber());
                List<MDesignProcedureModule> mDesignProcedureModule = this.mDesignProcedureModuleMapper.selectProcedureModule(mProcedureVo1.getDetailsNumber() + "", mProcedureVo.getProductId());
                mProcedureVo1.setDetailsNumber(z + 1);
                this.mManufactureMapper.insertProcedureReturnkey(mProcedureVo1);
                for (int j = 0; j < mDesignProcedureModule.size(); j++) {
                    MDesignProcedureModule procedureModule = mDesignProcedureModule.get(j);
                    Integer amount = procedureModule.getAmount();
                    procedureModule.setAmount(mProcedureVo.getAmount() * amount);
                    procedureModule.setSubtotal(mProcedureVo.getAmount() * amount * procedureModule.getCostPrice());
                    procedureModule.setParentId(mProcedureVo1.getId());
                    procedureModule.setDetailsNumber(j + 1);
                    this.mDesignProcedureModuleMapper.insertProcedureModule(procedureModule);
                }
            }
            String[] split = mProcedureVo.getApplyIdGroup().split("\\|");
            for (int k = 0; k < split.length; k++) {
                this.mapper.updateManufacture_tag(split[k]);
            }

            List<MDesignProcedureModuleVo> mDesignProcedureModuleVos = this.mDesignProcedureModuleMapper.queryModule_subtotal(mProcedureVo.getManufactureId());
            for (int i = 0; i < mDesignProcedureModuleVos.size(); i++) {
                this.mManufactureMapper.updateModule_subtotal(mDesignProcedureModuleVos.get(i).getParent_id(), mDesignProcedureModuleVos.get(i).getSum());
            }

            this.mManufactureMapper.updateManufactureSubtotal(s,sum*mProcedureVo.getAmount());


        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }
}
